<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell @click="cellClickFn">
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ artItem.title }}</span>
          <!-- 单图 -->
          <img v-if="artItem.cover.type === 1" class="thumb" alt="" v-lazy="artItem.cover.images[0]" />
          <!-- <van-image class="thumb" v-if="artItem.cover.type === 1" v-lazy="artItem.cover.images[0]">
            <template v-slot:error>加载失败</template>
          </van-image> -->
        </div>
        <!-- 三图 -->
        <div class="thumb-box" v-if="artItem.cover.type > 1">
          <img class="thumb" v-for="(imgUrl,index) in artItem.cover.images" :key="index" alt="" v-lazy="imgUrl">
          <!-- <van-image class="thumb" v-for="(imgUrl, index) in artItem.cover.images" :key="index" :src="imgUrl" v-lazy="imgUrl">
            <template v-slot:error>加载失败</template>
          </van-image> -->
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ artItem.aut_name }}</span>
            <span>{{ artItem.comm_count }}评论</span>
            <span>{{ formatTime(artItem.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show = true" v-if="isShow" />
        </div>
      </template>
    </van-cell>
    <!-- 反馈面板 -->
    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" get-container="body" :cancel-text="bottomText" @cancel="cancelFn" @closed="closed" />
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import { firstActions, secondActions } from '@/api/report.js'
export default {
  name: 'ArticleItem',
  props: {
    artItem: Object, // 文章的对象
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show: false, // 反馈面板显示/隐藏
      actions: firstActions, // 反馈面板选项数组
      bottomText: '取消' // 底部按钮的文字
    }
  },
  methods: {
    formatTime: timeAgo, // 函数体是timeAgo
    // 反馈面板 - “选项”选择事件
    onSelect (action, item) {
      // console.log(action) // 对象
      // console.log(item) // 索引值
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // this.show = false

      if (action.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.bottomText = '返回'
      } else if (action.name === '不感兴趣') {
        this.$emit('disLikeEV', this.artItem.art_id)
        this.show = false // 让反馈模块隐藏
      } else {
        // 二级反馈选项
        this.$emit('reportEV', this.artItem.art_id, action.value)
        this.show = false // 直接关闭反馈选项
      }
    },
    // 反馈面板 - 底部按钮-点击事件
    cancelFn () {
      if (this.bottomText === '返回') {
        this.show = true // 强制回来
        this.actions = firstActions
        this.bottomText = '取消'
      }
    },
    // 反馈面板 - 关闭面板-出发事件
    closed () {
      this.actions = firstActions
      this.bottomText = '取消'
    },
    // 文章单元格 - 点击事件
    cellClickFn () {}
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
